import { useState } from 'react';
import { history } from 'umi';

interface currentRouteProps {
  path?: string | undefined;
  name?: string | undefined;
}
class initialStateProps {
  currentRouteName: string;
  cachedRoutes: Array<currentRouteProps>;
  currentRoute: currentRouteProps;
  defaultPagePath: string;
  windowSize: string;
  scrollY: number;
  constructor() {
    this.currentRouteName = '';
    this.defaultPagePath = '/index';
    this.cachedRoutes = [];
    this.scrollY = 0;
    this.windowSize = 'xl';
    this.currentRoute = {
      path: '',
      name: '',
    };
  }
}
export type globalStateType = initialStateProps;

export default function useGlobalModel() {
  const [initialState, setInitialState] = useState<initialStateProps>(new initialStateProps());

  const setGlobalState = async (globalState: Partial<initialStateProps>) => {
    await setInitialState(() => ({ ...initialState, ...globalState }));
  };

  //设置默认全局数据
  const clearGlobalState = () => {
    setInitialState(() => new initialStateProps());
  };
  //关闭标签页面
  const closeTag = async (tag: string, drop: (arg0: string) => void) => {
    const { cachedRoutes, defaultPagePath, currentRoute } = initialState;
    const newCacheRoutes = cachedRoutes.filter((item) => {
      return tag !== item.path;
    });
    const newCurrentRoute =
      tag === currentRoute.path
        ? { currentRoute: newCacheRoutes[newCacheRoutes.length - 1] || defaultPagePath }
        : {};

    await setGlobalState({ cachedRoutes: newCacheRoutes, ...newCurrentRoute });

    if (Object.keys(newCurrentRoute).length !== 0) {
      const pagePath = newCacheRoutes[newCacheRoutes.length - 1]?.path || defaultPagePath;
      await history.replace(pagePath);
    }
    drop(tag);
  };
  //关闭其他标签页面
  const closeOtherTag = async (tag: string, drop: (arg0: string) => void) => {
    const { cachedRoutes } = initialState;
    const newCacheRoutes = cachedRoutes.filter((item) => {
      if (tag !== item.path) {
        drop(tag);
      }
      return tag === item.path;
    });
    const newCurrentRoute = { currentRoute: newCacheRoutes[0] };

    await setGlobalState({ cachedRoutes: newCacheRoutes, ...newCurrentRoute });
    await history.replace(tag);
  };
  //计算表格table的scrollY
  const calculationTableHeight = (
    props:
      | {
          searchHeight?: number;
          searchMarginBottom?: number;
          headerHeight?: number;
          pagingHeight?: number;
          tableHeaderHeight?: number;
        }
      | undefined,
  ) => {
    const {
      searchHeight = 60,
      searchMarginBottom = 16,
      headerHeight = 108,
      pagingHeight = 65,
      tableHeaderHeight = 51,
    } = props || {};
    const fn = () => {
      const bodyHeight = document.body.offsetHeight;
      const scrollY =
        bodyHeight -
        searchHeight -
        searchMarginBottom -
        tableHeaderHeight -
        headerHeight -
        pagingHeight;

      if (initialState.scrollY !== scrollY) {
        setInitialState(() => ({ ...initialState, scrollY }));
      }
    };
    fn();
    window.onresize = () => {
      fn();
    };
  };

  return {
    ...initialState,
    setGlobalState,
    clearGlobalState,
    closeTag,
    closeOtherTag,
    calculationTableHeight,
  };
}
